<template>
	<view class="page-bg upd-order" v-if="fm.ps">
		<view class="fm-title">
			<view class="title"><label class="line-icon" :style="{'background': '#00c3c3'}"></label>修改订单</view>
			<u-button :loading="fm.sb.load" type="success" size="medium" @click="updOrderSubmit"
				:custom-style="{'padding': '0rpx 10rpx'}">
				<u-icon v-if="!fm.sb.load" name="edit-pen-fill" color="#fff"
					:custom-style="{'margin-right': '10rpx'}"></u-icon>
				提交修改
			</u-button>
		</view>

		<u-form :model="fm.ps" :ref="fm.fname">
			<view class="my-fm">
				<view class="f-item">
					<view class="i-row">
						<view class="r-title">
							<label class="r-l-icon" :style="{'background': '#00c3ce'}"></label>
							开始时间
						</view>
						<view class="r-con">
							<MyDateTime v-if="fm.ps.startTime" :name="'startTime'" :dateTime="fm.ps.startTime"
								@myClick="setDateTime"></MyDateTime>
						</view>
					</view>

					<view class="i-row">
						<view class="r-title">
							<label class="r-l-icon" :style="{'background': '#2979ff'}"></label>
							结束时间
						</view>
						<view class="r-con">
							<MyDateTime v-if="fm.ps.endTime" :name="'endTime'" :dateTime="fm.ps.endTime"
								@myClick="setDateTime"></MyDateTime>
						</view>
					</view>
				</view>

				<view class="f-item">
					<view class="i-row">
						<view class="r-title">
							<label class="r-l-icon" :style="{'background': '#f00'}"></label>
							出发地点
						</view>
						<view class="r-con" @click="setMpMd({show: true, name: 'fromLocationData'})">
							<view class="c-text" v-if="fm.ps.fromLocationData">{{fm.ps.fromLocationData.address?fm.ps.fromLocationData.address:fm.ps.fromLocationData.name}}</view>
							<view class="i-r-text" v-else>请选择出发地点</view>
							<u-icon name="arrow-right" color="#999"></u-icon>
						</view>
					</view>
				</view>

			

				<view class="f-item">
					<view class="i-row">
						<view class="r-title">
							<label class="r-l-icon" :style="{'background': '#000'}"></label>
							到达地点
						</view>
						<view class="r-con" @click="setMpMd({show: true, name: 'toLocationData'})">
							<view class="c-text" v-if="fm.ps.toLocationData">{{fm.ps.toLocationData.address?fm.ps.toLocationData.address:fm.ps.toLocationData.name}}</view>
							<view class="i-r-text" v-else>请选择到达地点</view>
							<u-icon name="arrow-right" color="#999"></u-icon>
						</view>
					</view>
				</view>

			</view>
		</u-form>

		<MapSearchList v-if="mpMd.show" :name="mpMd.name" :nameIndex="mpMd.nameIndex" @myClick="setMpMd">
		</MapSearchList>
		<u-toast ref="qtoast" />
	</view>
</template>

<script>
	import MyDateTime from '@/components/my-date-time/my-date-time.vue';
	import MapSearchList from '@/components/map-search-list/map-search-list.vue';

	export default {
		components: {
			MyDateTime,
			MapSearchList
		},
		data() {
			return {
				// 订单行程天数
				drCount: 0,
				// 某天行程对象
				obj: null,

				// 地图弹框
				mpMd: {
					show: false,
					name: '',
					nameIndex: ''
				},

				// 表单对象
				fm: {
					fname: 'orderUpdFm',
					sb: {
						load: false,
						text: '修改',
						okBtnSty: this.C.subBtn
					},
					ps: null,
				}
			}
		},
		onLoad(e) {

			const that = this;
			that.$u.api.getOrderDetail({
				id: e.id
			}).then(res => {
				if (res.code == 1) {
					that.obj = res.data; // 保存起来，验证基础数据
					that.fm.ps = res.data;
					// console.log( res.data.startTime)
					that.fm.ps.startTime = {
						date: res.data.startTime.split(' ')[0],
						time: res.data.startTime.split(' ')[1]
					};
					that.fm.ps.endTime = {
						date: res.data.endTime.split(' ')[0],
						time: res.data.endTime.split(' ')[1]
					};
					
				}
			})


			// const eventChannel = that.getOpenerEventChannel();
			// eventChannel.on('pageData', function(res){
			// 	console.log('父页面传入的数据', res);

			// 	that.drCount = res.drCount;

			// that.obj = res.data;// 保存起来，验证基础数据
			// that.fm.ps = res.data;

			// 	console.log('修改订单表单参数对象', that.fm.ps);
			// });
		},
		methods: {
			// 打开地图地址选择列表
			setMpMd(e) {
				const that = this,
					md = that.mpMd,
					ps = that.fm.ps;

				if (e.show) {
					// 准备数据
					md.name = e.name;
					md.nameIndex = e.nameIndex;
				} else {
					console.log('子组件传入的数据', e);
					let p = e.obj;
					if (p) { // 存在地点数据对象
						if (e.nameIndex === "") {
							ps[e.name] = {
								// 地点id
								id: ps[e.name].id,
								// 坐标
								lnglat: p.lnglat,
								// 地址
								address: p.area.split('-')[1] + ' ' + p.name,
								// 区域
								area: p.area,
								// 是否站点
								isSta: p.isSta
							};
						} else {
							ps[e.name][parseInt(e.nameIndex)] = {
								// 地点id
								id: ps[e.name][parseInt(e.nameIndex)].id,
								// 坐标
								lnglat: p.lnglat,
								// 地址
								address: p.area.split('-')[1] + ' ' + p.name,
								// 区域
								area: p.area,
								// 是否站点
								isSta: p.isSta
							};
						}

					}
				}

				that.mpMd.show = e.show;
			},
			// 选择日期时间
			setDateTime(e) {
				// console.log('子组件的数据', e);

				// 回填日期时间数据
				this.fm.ps[e.name] = e.obj;
			},
			// 验证表单
			valFm(ps) {
				let that = this,
					U = that.U,
					FV = that.FV,
					obj = that.obj,
					fg = true,
					p = {};

				if (fg) {
					let orderNum = ps.orderNum;
					if (!orderNum) {
						fg = U.qterror('行程订单编号数据错误，请重新选择一天行程再次修改', that);
					} else {
						p.orderNum = orderNum;
					}
				}

				if (fg) {
					let id = ps.id;
					if (!id) {
						fg = U.qterror('行程id数据错误，请重新选择一天行程再次修改', that);
					} else {
						p.id = id;
					}
				}

				if (fg) {
					let startTime = ps.startTime.date;
					if (!startTime) {
						fg = U.qterror('行程开始时间不能为空', that);
					} else {
						p.startTime = `${ps.startTime.date} ${ps.startTime.time}`;
					}
				}

				if (fg) {
					let endTime = ps.endTime.date;
					if (!endTime) {
						fg = U.qterror('行程结束时间不能为空', that);
					} else {
						p.endTime = `${ps.endTime.date} ${ps.endTime.time}`;
					}
				}

				if (fg) {
					let sp = ps.fromLocationData;
					if (!sp.lnglat) {
						fg = U.qterror('行程开始地点不能为空', that);
					} else {
						p.fromLocationData = {
							id: sp.id,
							address: sp.address,
							lnglat: sp.lnglat,
							area: sp.area,
							isSta: sp.isSta
						};
					}
				}

				// if (fg) {
				// 	if (obj.amps.length > 0) {
				// 		let amps = [];
				// 		for (let i = 0; i < ps.amps.length; i++) {
				// 			let amp = ps.amps[i];
				// 			if (!amp.lnglat) {
				// 				fg = U.qterror('上午行程第' + (i + 1) + '个地点不能为空', that);
				// 				break;
				// 			} else {
				// 				amps.push({
				// 					id: amp.id,
				// 					address: amp.address,
				// 					lnglat: amp.lnglat,
				// 					area: amp.area,
				// 					isSta: amp.isSta
				// 				});
				// 			}
				// 		}

				// 		if (fg) p.ampoints = amps;
				// 	}
				// }

				// if (fg) {
				// 	if (ps.lunchPoint) {
				// 		let lp = ps.lunchPoint;
				// 		if (!lp.lnglat) {
				// 			fg = U.qterror('行程午餐地点不能为空', that);
				// 		} else {
				// 			p.lunchPoint = {
				// 				id: lp.id,
				// 				address: lp.address,
				// 				lnglat: lp.lnglat,
				// 				area: lp.area,
				// 				isSta: lp.isSta
				// 			};
				// 		}
				// 	}
				// }

				// if (fg) {
				// 	if (obj.pmps.length > 0) {
				// 		let pmps = [];
				// 		for (let i = 0; i < ps.pmps.length; i++) {
				// 			let pmp = ps.pmps[i];
				// 			if (!pmp.lnglat) {
				// 				fg = U.qterror('下午行程第' + (i + 1) + '个地点不能为空', that);
				// 				break;
				// 			} else {
				// 				pmps.push({
				// 					id: pmp.id,
				// 					address: pmp.address,
				// 					lnglat: pmp.lnglat,
				// 					area: pmp.area,
				// 					isSta: pmp.isSta
				// 				});
				// 			}
				// 		}

				// 		if (fg) p.pmpoints = pmps;
				// 	}
				// }

				if (fg) {
					let ep = ps.toLocationData;
					if (!ep.lnglat) {
						fg = U.qterror('行程结束地点不能为空', that);
					} else {
						p.toLocationData = {
							id: ep.id,
							address: ep.address,
							lnglat: ep.lnglat,
							area: ep.area,
							isSta: ep.isSta
						};
					}
				}

				return fg ? p : fg;
			},
			// 提交-请假修改-表单
			updOrderSubmit() {
				let that = this,
					U = that.U,
					ps = U.scopy(that.fm.ps),
					sb = that.fm.sb;
				console.log('处理提交参数前', ps);

				// 验证参数
				ps = that.valFm(ps);
				if (ps != false) {
					console.log('最终参数',JSON.stringify(ps));
					
					sb.load = true;
					ps.type = that.obj.type
					that.$u.api.updCarOrder(ps).then((res) => {
						if (res.code != 1) {
							sb.load = false;
						} else {
							U.qtoast(that, {
								title: res.msg,
								type: 'success',
								duration: 1000,
								callback: function() {
									sb.load = false;

									// 调用父级onLoad中的刷新方法
									if (that.drCount > 1) {
										uni.$emit('refOrderDayRouteList');
									} else {
										uni.$emit('refOrderList');
									}

									// 返回上一页
									uni.navigateBack({
										delta: 1
									});
								}
							});
						}
					}).catch(err => {
						sb.load = false;
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.upd-order {
		padding-top: 90rpx;
		padding-bottom: 10rpx;
	}
</style>